package com.neuedu.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.model.Admin;
import com.neuedu.service.AdminService;

@Controller
@RequestMapping(value="/admin")
public class AdminAction {

	@Autowired
	private AdminService adminService;
	
	@RequestMapping("/login")
	public void login(Admin admin,
			    HttpServletRequest request,HttpServletResponse response) throws Exception{
		String name = request.getParameter("admin");
		admin.setAdmin(name);
		System.out.println("========="+admin.getAdmin()+"======"+admin.getPassword());
		
		//调用业务逻辑层进行处理，进行用户的登录
		Admin ret = adminService.findUserMessageByName(admin);
		if(ret!=null){
			//登录成功 - 记录用户的session会话中，并跳转Dao管理首页
			request.getSession().setAttribute("LOGIN_USER", ret);
			
			response.sendRedirect(request.getContextPath()+"/admin/index.jsp");
		}else{
			//登录失败,返回登录页面
			System.out.println("==============="+ret.getAdmin());
			response.sendRedirect(request.getContextPath()+"/login.jsp");
		}	
	}
	
	@RequestMapping("/logout")
	public void logout( HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	//使会话失效
	request.getSession().invalidate();	

	//返回登录界面
	response.sendRedirect(request.getContextPath()+"/login.jsp");
	}
	
	
}
